package libjdc.dc.util;

import java.text.DecimalFormat;

public class PowerOfByte {
	
	private static final String[] ARRAY = {
		"Bytes","KiB","MiB","GiB","TiB"};
	
	public static final double UMKILOBYTE = 1024.0;
	public static final double UMMEGABYTE = UMKILOBYTE * 1024.0;
	public static final double UMGIGABYTE = UMMEGABYTE * 1024.0;
	public static final double UMTERABYTE = UMGIGABYTE * 1024.0;

	private static final double[] ARRAY_VAL = {
		1,UMKILOBYTE,UMMEGABYTE,UMGIGABYTE,UMTERABYTE};
	
	public static String convert(long val){
		int power;
		if(val < UMKILOBYTE)
			power = 0;
		else if(val < UMMEGABYTE)
			power = 1;
		else if(val < UMGIGABYTE)
			power = 2;
		else if(val < UMTERABYTE)
			power = 3;
		else
			power = 4;
		DecimalFormat myFormatter = new DecimalFormat("###,###.###");
		return myFormatter.format((val/ARRAY_VAL[power]))+" "+ARRAY[power];
	}

}
